const express = require('express');
const router = express.Router();
const query = require("../util/db.js")
const R = require("../util/resultJson.js")

router.get("/",function (req,res) {
    res.render("goods/goods-list")
})

router.get("/edit",function (req,res) {
    res.render("goods/goods-edit")
});

router.get("/info/:id",function (req,res) {
    let sql = "select * from goods where id = ?";
    query(sql,[req.params.id]).then(goods=>{
        if(goods.length > 0){
            sql = "select * from goods_store where goods_id = ?";
            query(sql,[req.params.id]).then(goodsStore=>{
                sql = "select * from goods_image where goods_id = ?";
                query(sql,[req.params.id]).then(goodsImage=>{
                    res.json(R.success({
                        goods:goods[0],
                        goodsStore,
                        goodsImage
                    }))
                })
            })
        }else {
            res.json(R.error("编号异常！"))
        }
    })
})

router.get("/page",function (req,res) {
    const {page,limit,name,supplierId,expire,status} = req.query;
    let sql = "select g.*,s.name supplierName,u.nickname createName from goods g left join supplier s on g.supplier_id = s.id left join userinfo u on g.create_id = u.id where 1 = 1 ";
    let objs = [];
    if(name){
        sql+="and g.name like ? ";
        objs.push("%"+name+"%");
    }
    if(supplierId){
        sql+="and g.supplier_id = ? ";
        objs.push(supplierId);
    }
    if(expire){
        const rangeDate = expire.split(" - ");
        sql+='and g.expire between ? and ? ';
        objs.push(rangeDate[0])
        objs.push(rangeDate[1])
    }
    if(status){
        sql+="and g.status = ? ";
        objs.push(status)
    }
    query(sql,objs).then(list=>{
        const count = list.length;
        sql+="order by g.id desc limit ?,?";
        objs.push((page-1)*limit);
        objs.push(parseInt(limit));
        query(sql,objs).then(data=>{
            res.json(R.page(count,data));
        })
    })
})

router.post("/update",function (req,res) {
    const goods = req.body;
    if(goods.id){
        let sql = "update goods set name = ?,profile = ?,count = ?,expire = ?,supplier_id = ?,status = ? where id = ?";
        query(sql,[goods.name,goods.profile,goods.count,goods.expire,goods.supplierId,goods.status,goods.id]).then(()=>{
            /*删除仓库信息*/
            sql = "delete from goods_store where goods_id = ?";
            query(sql,[goods.id]).then(()=>{
                /*处理仓库信息*/
                const storeIds = goods.storeIds.split(",");
                const storeObjs = [];
                let storeSql = 'insert into goods_store values';
                for(const storeId of storeIds){
                    storeSql+='(null,?,?),';
                    storeObjs.push(goods.id);
                    storeObjs.push(storeId);
                }
                storeSql=storeSql.substring(0,storeSql.length-1);//去除最后的逗号
                query(storeSql,storeObjs).then(()=>{
                    /*删除图片信息*/
                    sql = "delete from goods_image where goods_id = ?";
                    query(sql,[goods.id]).then(()=>{
                        /*处理图片信息*/
                        const goodsImage = goods.goodsImage.split(",");
                        const imgObjs = [];
                        let imgSql = "insert into goods_image values";
                        for(const src of goodsImage){
                            imgSql+='(null,?,?,?),';
                            imgObjs.push(goods.id);
                            imgObjs.push(src)
                            imgObjs.push(src)
                        }
                        imgSql=imgSql.substring(0,imgSql.length-1);//去除最后的逗号
                        query(imgSql,imgObjs).then(()=>{
                            res.json(R.success());
                        })
                    })
                })
            })
        })
    }else{
        let sql = "insert into goods values(null,?,?,?,?,?,?,?)";
        query(sql,[goods.name,goods.profile,goods.count,goods.expire,goods.supplierId,req.loginInfo.id,goods.status]).then(r=>{
            /*处理仓库信息*/
            const storeIds = goods.storeIds.split(",");
            const storeObjs = [];
            let storeSql = 'insert into goods_store values';
            for(const storeId of storeIds){
                storeSql+='(null,?,?),';
                storeObjs.push(r);
                storeObjs.push(storeId);
            }
            storeSql=storeSql.substring(0,storeSql.length-1);//去除最后的逗号
            query(storeSql,storeObjs).then(()=>{
                /*处理图片信息*/
                const goodsImage = goods.goodsImage.split(",");
                const imgObjs = [];
                let imgSql = "insert into goods_image values";
                for(const src of goodsImage){
                    imgSql+='(null,?,?,?),';
                    imgObjs.push(r);
                    imgObjs.push(src)
                    imgObjs.push(src)
                }
                imgSql=imgSql.substring(0,imgSql.length-1);//去除最后的逗号
                query(imgSql,imgObjs).then(()=>{
                    res.json(R.success());
                })
            })
        })
    }
})

router.delete("/delete/:id",function (req,res) {
    let sql = "delete from goods where id = ?";
    query(sql,[req.params.id]).then(()=>{
        sql = "delete from goods_store where goods_id = ?";
        query(sql,[req.params.id]).then(()=>{
            sql = "delete from goods_image where goods_id = ?";
            query(sql,[req.params.id]).then(()=>{
                res.json(R.success());
            })
        })
    })

})

router.post("/status",function (req,res) {
    let sql = "update goods set status = ? where id = ?";
    query(sql,[req.body.status,req.body.id]).then(r=>{
        res.json(R.success())
    })
})

router.get("/select",function (req,res) {
    query("select * from goods order by id desc").then(list=>{
        res.json(R.success(list))
    })
})

module.exports = router;
